/**
 * Copyright &copy; 2012-2016 <a href="https://istep.lczyfz.com">Istep</a> All rights reserved.
 */
package com.lczyfz.istep.modules.resource.service;

import com.lczyfz.istep.common.persistence.Page;
import com.lczyfz.istep.common.service.CrudService;
import com.lczyfz.istep.modules.resource.dao.ResourceCourseCoursesystemDao;
import com.lczyfz.istep.modules.resource.dao.ResourceTrainprogramPaperDao;
import com.lczyfz.istep.modules.resource.entity.ResourceCourseCoursesystem;
import com.lczyfz.istep.modules.resource.entity.ResourceCoursesystem;
import com.lczyfz.istep.modules.resource.entity.ResourceTrainprogram;
import com.lczyfz.istep.modules.resource.entity.ResourceTrainprogramPaper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 培训方案-试卷中间表Service
 * @author dh
 * @version 2017-02-21
 */
@Service
@Transactional(readOnly = true)
public class ResourceTrainprogramPaperService extends CrudService<ResourceTrainprogramPaperDao, ResourceTrainprogramPaper> {


	public ResourceTrainprogramPaper get(String id) {
		return super.get(id);
	}
	
	public List<ResourceTrainprogramPaper> findList(ResourceTrainprogramPaper resourceTrainprogramPaper) {
		return super.findList(resourceTrainprogramPaper);
	}
	
	public Page<ResourceTrainprogramPaper> findPage(Page<ResourceTrainprogramPaper> page, ResourceTrainprogramPaper resourceTrainprogramPaper) {
		return super.findPage(page, resourceTrainprogramPaper);
	}
	
	@Transactional(readOnly = false)
	public void save(String duallist,ResourceTrainprogram resourceTrainprogram) {
		String[] resourcePaperList = new String[0];
		if (duallist != null && !duallist.equals(""))
			resourcePaperList = duallist.split(",");
		logger.debug("length: [{}]", resourcePaperList.length);

		for (int i = 0; i < resourcePaperList.length; i++) {

			ResourceTrainprogramPaper resourceTrainprogramPaper=new ResourceTrainprogramPaper();
			resourceTrainprogramPaper.setPaperId(resourcePaperList[i]);
			resourceTrainprogramPaper.setTrainprogramId(resourceTrainprogram.getId());
			super.save(resourceTrainprogramPaper);
		}


	}
	@Transactional(readOnly = false)
	public void update(String duallist,ResourceTrainprogram resourceTrainprogram) {
		ResourceTrainprogramPaper temp=new ResourceTrainprogramPaper();
		temp.setTrainprogramId(resourceTrainprogram.getId());
		List<ResourceTrainprogramPaper> trainprogramPaperList=super.findList(temp);

		for(int i=0;i<trainprogramPaperList.size();i++)
		{
			super.delete(trainprogramPaperList.get(i));
		}
		String[] resourcePaperList = new String[0];
		if (duallist != null && !duallist.equals(""))
			resourcePaperList = duallist.split(",");
		logger.debug("length: [{}]", resourcePaperList.length);
		for (int i = 0; i < resourcePaperList.length; i++) {

			ResourceTrainprogramPaper resourceTrainprogramPaper=new ResourceTrainprogramPaper();
			resourceTrainprogramPaper.setTrainprogramId(resourceTrainprogram.getId());
			resourceTrainprogramPaper.setPaperId(resourcePaperList[i]);
			super.save(resourceTrainprogramPaper);
		}
	}
	@Transactional(readOnly = false)
	public void delete(ResourceTrainprogramPaper resourceTrainprogramPaper) {
		super.delete(resourceTrainprogramPaper);
	}
	
}